package com.eportal.action.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.CBuilding;
import com.eportal.domain.CInstallUnit;
import com.eportal.domain.CRoom;
import com.eportal.domain.Sysopt;
import com.eportal.domain.Sysroles;
import com.eportal.service.CBuildingService;
import com.eportal.service.CInstallUnitService;
import com.eportal.service.CRoomService;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 单元管理
 * @author chencheng
 *
 */
public class CInstallUnitAction extends BaseAction implements ModelDriven<CInstallUnit> {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;


    private Map<String, Object> result = new HashMap<String, Object>();
    
    @Autowired
    private CInstallUnitService cInstallUnitService;
    @Autowired
    private CBuildingService cBuildingService;
    
    @Autowired
    private CRoomService cRoomService;
	

	/**
     * 单元管理
     */
    @Deprecated
    public String listView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	if (userRole == 1) {
    		getRequest().setAttribute("communityId", getRequest().getParameter("communityId"));
    	}else{
    		getRequest().setAttribute("communityId", "");
    		
    	}
        return SUCCESS;
    }
    
	/**
     * 单元管理
     */
    public String listViewV2() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	if (userRole == 1) {
    		getRequest().setAttribute("communityId", getRequest().getParameter("communityId"));
    	}else{
    		getRequest().setAttribute("communityId", "");
    		
    	}
        return SUCCESS;
    }
    
    /**
     * 单元列表
     */
    @Deprecated
    public void list() throws Exception {
        JqGridPager page = new JqGridPager();
        setPage(getRequest(), page);
        Sysopt  user= (Sysopt)getSession().getAttribute("user");
        String communityId = getRequest().getParameter("communityId");
	 	if (StringUtils.isBlank(communityId)) {
	 		communityId = user.getCommunityId();
		}
        List<CInstallUnit> cInstallUnits = cInstallUnitService.list(page,communityId);
        jqGridQueryMap(Sysroles.class, getRequest(), cInstallUnits, getResponse(), page);
    }
    
    /**
     * 单元列表
     * 20170327 楼栋管理升级 
     * wangk
     */
    public void listV2() throws Exception {
        JqGridPager page = new JqGridPager();
        Sysopt  user= (Sysopt)getSession().getAttribute("user");
        String communityId = getRequest().getParameter("communityId");
	 	if (StringUtils.isBlank(communityId)) {
	 		communityId = user.getCommunityId();
		}
	 	String cBuildingId = getRequest().getParameter("cBuildingId");
    	String unitId = getRequest().getParameter("unitId");
    	String roomNo = getRequest().getParameter("roomNo");
    	String ownerName = getRequest().getParameter("ownerName");
    	String tel = getRequest().getParameter("tel");
    	String idNo = getRequest().getParameter("idNo");
	 	
    	StringBuilder where = new StringBuilder(" t.communityid='");
    	where.append(communityId);
    	where.append("'");
    	if (!StringUtils.isBlank(cBuildingId)) {
    		where.append(" and b.sid = '");
    		where.append(cBuildingId);
    		where.append("'");
    	}
    	if (!StringUtils.isBlank(unitId)) {
    		where.append(" and u.sid = '");
    		where.append(unitId);
    		where.append("'");
    	}
    	if (!StringUtils.isBlank(roomNo)) {
    		where.append(" and t.room_no like '%");
    		where.append(roomNo);
    		where.append("%'");
    	}
    	if (!StringUtils.isBlank(ownerName)) {
    		where.append(" and t.owner_name like '%");
    		where.append(ownerName);
    		where.append("%'");
    	}
    	if (!StringUtils.isBlank(tel)) {
    		where.append(" and t.owner_tel = '");
    		where.append(tel);
    		where.append("'");
    	}
    	if (!StringUtils.isBlank(idNo)) {
    		where.append(" and t.owner_id_number like '%");
    		where.append(idNo);
    		where.append("%'");
    	}
    	page.setWhere(where.toString());
    	setPage(getRequest(), page);
        List<Map<String, Object>> cRooms = cRoomService.listPage(page);
        jqGridQueryMap(Sysroles.class, getRequest(), cRooms, getResponse(), page);
    }
    
    /**
     * 楼栋下单元列表
     */
    public void listUnit() throws Exception {
    	String bsid = getRequest().getParameter("bsid");
    	String type = getRequest().getParameter("type");
    	String unionId = getRequest().getParameter("unionId");
    	if ("first".equals(bsid)) {
    		Sysopt  user= (Sysopt)getSession().getAttribute("user");
    		StringBuilder where = new StringBuilder(" where 1=1 ");
    		where.append(" and communityId ='");
    		where.append(user.getCommunityId());
    		where.append("'");
    		List<CBuilding> CBuildings = cBuildingService.list(where.toString());
    		if (CBuildings.size() <1) {
    			responseBody(CBuildings);
    			return;
			}
    		List<CInstallUnit> cInstallUnits = null;
    		for (CBuilding cBuilding : CBuildings) {
    			StringBuilder where1 = new StringBuilder("  where cBuildingId ='");
    			where1.append(cBuilding.getSid());
    			where1.append("'");
    	    	List<CInstallUnit> cInstallUnits1 =  cInstallUnitService.list(where1.toString());
    	    	if (cInstallUnits == null) {
    	    		cInstallUnits = cInstallUnits1;
				}else{
					cInstallUnits.addAll(cInstallUnits1);
    	    	}
			}
    		responseBody(cInstallUnits);
    		return;
		}
    	StringBuilder where = new StringBuilder("select * from t032_cinstallunit t where t.cBuildingId ='");
		where.append(bsid);
		where.append("' ");
//		where.append("and  not exists (select l.installunitId from t05_locks l where t.sid=l.installunitId)");
//    	if ("edit".equals(type)) {
//    		if (StringUtils.isNotBlank(unionId)) {
//				where.append(" union all (select * from t032_cinstallunit t where t.sid=");
//				where.append(unionId);
//				where.append(")");
//			}
//		}
		List<Map<String, String>> cInstallUnits =  cInstallUnitService.listBySql(where.toString());
    	responseBody(cInstallUnits);
    }
    
    /**
     * 所有的楼栋单元列表
     */
    public void listBudingAndUnit() throws Exception {
    	String communityId = getRequest().getParameter("communityId");
   	 	Sysopt  user= (Sysopt)getSession().getAttribute("user");
   	 	if (StringUtils.isBlank(communityId)) {
   	 		communityId = user.getCommunityId();
		}
    	List<Map<String, Object>> listBudingAndUnit = cInstallUnitService.queryBySql(communityId);
        responseBody(listBudingAndUnit);
    }
    
    /**
     * 单元新增
     */
    public String addView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	if (userRole == 1) {
    		getRequest().setAttribute("communityId", getRequest().getParameter("communityId"));
    	}else{
    		getRequest().setAttribute("communityId", "");
    	}
        return SUCCESS;
    }
    
    /**
     * 楼栋新增保存
     */
    public void add(){
    	CInstallUnit cInstallUnit = getModel();
    	String cBuildingId= getRequest().getParameter("cBuildingId");
    	cInstallUnit.setcBuildingId(cBuildingId);
    	result = cInstallUnitService.addMethod(cInstallUnit);
        responseBody(result);
    }
    
    /**
     * 单元修改
     */
    public String editView() {
    	Map<String, String> map =  cInstallUnitService.getCInstallUnit(getModel().getSid());
         getRequest().setAttribute("cInstallUnit", map);
        return SUCCESS;
    }
    
    /**
     * 楼栋更新保存
     */
    public void update(){
    	CInstallUnit cInstallUnit = getModel();
    	result = cInstallUnitService.updateMethod(cInstallUnit);
        responseBody(result);
    }

    /**
     * 单元删除
     */
    public void delete() {
    	String param_ids = getRequest().getParameter("ids");
    	result = cInstallUnitService.deleteMethod(param_ids);
    	cInstallUnitService.dateteUselessBuild();
        responseBody(result);
    }
    
    private CInstallUnit cInstallUnit = new CInstallUnit();

    @Override
    public CInstallUnit getModel() {
        return cInstallUnit;
    }
}
